var ServerIP = "http://127.0.0.1" ;

//regTest
getType = function(o){
	var _t;
	return ((_t = typeof(o)) == "object" ? Object.prototype.toString.call(o).slice(8,-1):_t).toLowerCase();
}
dfsObject = function(ele){
	var type = getType(ele);
	if(type == 'object'||type == 'array'){
		for(var key in ele){
			console.log(key);
			dfsObject(ele[key]);
		}
	}
	else{
		console.log(ele);
	}
}

/*后 加 前*/
function doReg(account,password,nickname,mobile,email){
	console.log('doReg:'+account+'~~'+password+'@@'+nickname+'~~'+mobile+'~~'+email);
	jQuery.ajax({
		url:'/regPdf',
		type:'post',
		data:{
			id:account,
			password:password,
			nickname:nickname,
			mobile:mobile,
			email:email
		},
	/*
			reply的可能结构
			{
				status:'success',
				user:Object(表示注册成功后得到的新用户对象)
			}
			{
				status:'error',
				message:string(失败的原因描述)
			}
			*/
		success:function(reply){
			console.log('ajax success');
			dfsObject(reply);
		},
		error:function(reply){
			console.log('ajax error');
			for(var key in reply){
				console.log(key+'='+reply[key])
			}
		}
	})
}
function doLogin(account,password){
	console.log('doLogin:'+account+'~~'+password);
	//登录成功后获得用户的token,token是用户与服务器数据通信的钥匙
	jQuery.ajax({
		url:'/loginPdf',
		type:'post',
		data:{
			id:account,
			password:password
		},
		success:function(reply){
			console.log('ajax success');
			dfsObject(reply);
		},
		error:function(reply){
			console.log('ajax error');
			for(var key in reply){
				console.log(key+'='+reply[key])
			}
		}
	})
}
function getUserInfo(userId,token){
	//用户根据token获取自身的最新信息
	console.log('getUserInfo:'+userId+'~~'+token);
	jQuery.ajax({
		url:'/getUserInfo?userId='+userId+'&token='+token,
		type:'get',
		success:function(reply){
			console.log('ajax success');
			dfsObject(reply);
		},
		error:function(reply){
			console.log('ajax error');
			for(var key in reply){
				console.log(key+'='+reply[key])
			}
		}
	})
}
function getTargetInfo(userId,token,targetId){
	//用户userId带上自己的token获取targetId对应的用户的简要信息
	//主要用于显示评论者的昵称和头像
	console.log('getTargetInfo:'+userId+'~~'+token+'~~'+targetId);
	jQuery.ajax({
		url:'/getTargetInfo?userId='+userId+'&token='+token+'&targetId='+targetId,
		type:'get',
		success:function(reply){
			console.log('ajax success');
			dfsObject(reply);
		},
		error:function(reply){
			console.log('ajax error');
			for(var key in reply){
				console.log(key+'='+reply[key])
			}
		}
	})
}
function updateHead(account,token){
	//在html中写一个表单，id=updateHeadForm,表单中有如下input标签
	//0:type=file&name=file
	//1:type=text&name=account&id=updateHeadAccount
	//2:type=text&name=token&id=updateHeadToken
	var updateHeadForm = document.getElementById('updateHeadForm');
	var accountInput = document.getElementById('updateHeadAccount');
	accountInput.value = account;
	var tokenInput = document.getElementById('updateHeadToken');
	tokenInput.value = token;
	var xhr = new XMLHttpRequest();
	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4){
			if(xhr.status == 200){
				var reply = jQuery.parseJSON(xhr.responseText);
				dfsObject(reply);
			}
		}
	}
	xhr.open('POST','/updateHead');
	xhr.send(new FormData(updateHeadForm));
}
function updateNickname(account,token,nickname){
	console.log('updateNickname:'+account+'~~'+token+'~~'+nickname);
	jQuery.ajax({
		url:'/updateNickname',
		type:'post',
		data:{
			account:account,
			token:token,
			nickname:nickname
		},
		success:function(reply){
			console.log('ajax success');
			dfsObject(reply);
		},
		error:function(reply){
			console.log('ajax error');
			for(var key in reply){
				console.log(key+'='+reply[key])
			}
		}
	})
}
function replyToPdf(pdfUrl,pageIndex,userId,token){
	//在html中写一个表单，id=replyForm,表单中有一个type=file&name=file
	//的input框，另外还有一些type=hidden和type=text的输入框
	//0:type=hidden&name=pdfUrl&id=replyPdfUrl
	//1:type=hidden&name=pageIndexl&id=replyPageIndex
	//2:type=hidden&name=userId&id=replyUserId
	//3:type=hidden&name=token&id=replyToken
	//4:type=radio&name=type
	//5:type=text&name=title
	//6:type=text&name=content
	console.log('replyToPdf:'+pdfUrl+'~~'+pageIndex+'~~'+userId+'~~'+token);
	var replyForm = document.getElementById('replyForm');
	var pdfUrlInput = document.getElementById('replyPdfUrl');
	pdfUrlInput.value = pdfUrl;
	var pageIndexInput = document.getElementById('replyPageIndex');
	pageIndexInput.value = pageIndex;
	var accountInput = document.getElementById('replyUserId');
	accountInput.value = userId;
	var tokenInput = document.getElementById('replyToken');
	tokenInput.value = token;
	var xhr = new XMLHttpRequest();
	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4){
			if(xhr.status == 200){
				var reply = jQuery.parseJSON(xhr.responseText);
				dfsObject(reply);
			}
		}
	}
	xhr.open('POST','/replyToPdf');
	xhr.send(new FormData(replyForm));
}
function praiseToReply(pdfUrl,pageIndex,replyId,userId,token){
	console.log('praiseToReply:'+pdfUrl+'~~'+pageIndex+'~~'+replyId+'~~'+userId+'~~'+token);
	jQuery.ajax({
		url:'/praiseToReply',
		type:'post',
		data:{
			pdfUrl:pdfUrl,
			pageIndex:pageIndex,
			replyId:replyId,
			userId:userId,
			token:token
		},
		success:function(reply){
			console.log('ajax success');
			dfsObject(reply);
		},
		error:function(reply){
			console.log('ajax error');
			for(var key in reply){
				console.log(key+'='+reply[key])
			}
		}
	})
}
function subReplyToReply(pdfUrl,pageIndex,replyId,content,userId,token){
	//replyId指回复的提问或回答的_id值
	console.log('subReplyToReply:'+pdfUrl+'~~'+pageIndex+'~~'+replyId+'~~'+content+'~~'+userId);
	jQuery.ajax({
		url:'/subReplyToReply',
		type:'post',
		data:{
			pdfUrl:pdfUrl,
			pageIndex:pageIndex,
			replyId:replyId,
			content:content,
			userId:userId,
			token:token,
		},
		success:function(reply){
			console.log('ajax success');
			dfsObject(reply);
		},
		error:function(reply){
			console.log('ajax error');
			for(var key in reply){
				console.log(key+'='+reply[key])
			}
		}
	})
}
function praiseToSubReply(pdfUrl,pageIndex,replyId,subReplyId,userId,token){
	console.log('praiseToReply:'+pdfUrl+'~~'+pageIndex+'~~'+replyId+'~~'+subReplyId+'~~'+userId+'~~'+token);
	jQuery.ajax({
		url:'/praiseToSubReply',
		type:'post',
		data:{
			pdfUrl:pdfUrl,
			pageIndex:pageIndex,
			replyId:replyId,
			subReplyId:subReplyId,
			userId:userId,
			token:token
		},
		success:function(reply){
			console.log('ajax success');
			dfsObject(reply);
		},
		error:function(reply){
			console.log('ajax error');
			for(var key in reply){
				console.log(key+'='+reply[key])
			}
		}
	})
}
function commentToSubReply(pdfUrl,pageIndex,replyId,subReplyId,content,target,userId,token){
	//replyId同上，subReplyId指子评论的_id值
	console.log('commentToSubReply:'+pdfUrl+'~~'+pageIndex+'~~'+replyId+'~~'+subReplyId
		+'~~'+content+'~~'+target+'~~'+userId);
	jQuery.ajax({
		url:'/commentToSubReply',
		type:'post',
		data:{
			pdfUrl:pdfUrl,
			pageIndex:pageIndex,
			replyId:replyId,
			subReplyId:subReplyId,
			content:content,
			userId:userId,
			token:token,
			target:target
		},
		success:function(reply){
			console.log('ajax success');
			dfsObject(reply);
		},
		error:function(reply){
			console.log('ajax error');
			for(var key in reply){
				console.log(key+'='+reply[key])
			}
		}
	})
}
function getReplyList(pdfUrl,pageIndex){
	//获取pdfUrl对应的第pageIndex页的回复列表
	console.log('getReplyList:'+pdfUrl+'~~'+pageIndex);
	jQuery.ajax({
		url:'/getReplyList?pdfUrl='+pdfUrl+'&pageIndex='+pageIndex,
		type:'get',
		success:function(reply){
			console.log('ajax success');
			dfsObject(reply);
		},
		error:function(reply){
			console.log('ajax error');
			for(var key in reply){
				console.log(key+'='+reply[key])
			}
		}
	})
}
function getReply(pdfUrl,pageIndex,replyId){
	//获取pdfUrl对应的第pageIndex页的replyId对应的回复
	console.log('getReply:'+pdfUrl+'~~'+pageIndex+'~~'+replyId);
	jQuery.ajax({
		url:'/getReply?pdfUrl='+pdfUrl+'&pageIndex='+pageIndex+'&replyId='+replyId,
		type:'get',
		success:function(reply){
			console.log('ajax success');
			dfsObject(reply);
		},
		error:function(reply){
			console.log('ajax error');
			for(var key in reply){
				console.log(key+'='+reply[key])
			}
		}
	})
}
function getSubReply(pdfUrl,pageIndex,replyId,subReplyId){
	//获取pdfUrl对应的第pageIndex页的replyId回复的subReplyId对应的子评论
	console.log('getSubReply:'+pdfUrl+'~~'+pageIndex+'~~'+replyId+'~~'+subReplyId);
	jQuery.ajax({
		url:'/getSubReply?pdfUrl='+pdfUrl+'&pageIndex='+pageIndex+'&replyId='+replyId+'&subReplyId='+subReplyId,
		type:'get',
		success:function(reply){
			console.log('ajax success');
			dfsObject(reply);
		},
		error:function(reply){
			console.log('ajax error');
			for(var key in reply){
				console.log(key+'='+reply[key])
			}
		}
	})
}